using DG.Tweening;
using Newtonsoft.Json;
using System.IO;
using System.Net;
using UnityEngine;
using UnityEngine.UI;

public class AssemblyPlayer : MonoBehaviour
{
    Sequence quence1;
    Sequence quence2;
    GameObject part;
    PartInfo partInfo;
    public Slider slider;

    // Start is called before the first frame update
    void Start()
    {
        string json = File.ReadAllText(Application.streamingAssetsPath + "/test.json");
        AllStep allStep = JsonConvert.DeserializeObject<AllStep>(json);
        Debug.Log(allStep.stepInfos[0].stepName);
        quence1 = DOTween.Sequence();
        quence1.SetAutoKill(false);
        quence1.Pause();
        quence1.OnComplete(() =>
        {
            Debug.Log("1complete");
        });

        quence2 = DOTween.Sequence();
        quence2.SetAutoKill(false);
        quence2.Pause();
        quence2.OnComplete(() =>
        {
            Debug.Log("2complete");
        });

        for (int i = 0; i < allStep.stepInfos.Count; i++)
        {
            StepInfo stepInfo = allStep.stepInfos[i];
            for (int j = 0; j < stepInfo.partInfos.Count; j++)
            {
                partInfo = stepInfo.partInfos[j];
                part = GameObject.Find(partInfo.modelName);
                if (part != null)
                {
                    //Tween tween2 = part.transform.DORotateQuaternion(partInfo.GetRotTo(), partInfo.lerpTime);
                    Tween tween2 = DOTween.To(() => part.transform.localEulerAngles, x => part.transform.localEulerAngles = x, partInfo.GetRotTo(), partInfo.lerpTime);
                    quence1.Append(tween2);

                    Tween tween1 = part.transform.DOLocalMove(partInfo.GetPosTo(), partInfo.lerpTime);
                    quence1.Append(tween1);

                    //Tween tween4 = part.transform.DORotateQuaternion(partInfo.GetRotFrom(), partInfo.lerpTime);
                    Tween tween4 = DOTween.To(() => part.transform.localEulerAngles, x => part.transform.localEulerAngles = x, partInfo.GetRotFrom(), partInfo.lerpTime);
                    //part.transform.DORotateQuaternion(partInfo.GetRotFrom(), partInfo.lerpTime);
                    quence2.Append(tween4);

                    Tween tween3 = part.transform.DOLocalMove(partInfo.GetPosFrom(), partInfo.lerpTime);
                    quence2.Append(tween3);
                }
            }
        }
        slider.onValueChanged.AddListener(OnValueChanged);
        //slider.OnPointerEnter
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 100, 50), "Play+"))
        {
            quence1.Restart();
            quence1.Play();
        }
        if (GUI.Button(new Rect(10, 60, 100, 50), "Play-"))
        {
            quence2.Restart();
            quence2.Play();
        }

        if (GUI.Button(new Rect(10, 110, 100, 50), "Pause2"))
        {
            //DOTween.Pause(quence2);
            quence2.Pause();
        }
        if (GUI.Button(new Rect(10, 160, 100, 50), "Restart2"))
        {
            //DOTween.Play(quence2);
            quence2.Play();
        }

        if (GUI.Button(new Rect(10, 210, 100, 50), "Forward"))
        {
            elapsedTime = 0;
            isPlay = true;
        }
        if (GUI.Button(new Rect(10, 260, 100, 50), "Back"))
        {
            elapsedTime = 0;
            isBack = true;
        }

        if (GUI.Button(new Rect(10, 310, 100, 50), "Pause"))
        {
            isPause = !isPause;
        }
    }

    bool isPause = false;
    bool isPlay = false;
    bool isBack = false;

    private float elapsedTime = 0f;
    public float moveDuration = 2f;
    private void Update()
    {
        if (isPlay&&!isPause)
        {
            elapsedTime += Time.deltaTime;
            if (elapsedTime >= moveDuration)
            {
                isPlay = false;
                // 移动完成
                Debug.Log("forward move complete");
            }
            else
            {
                // 在起始点和结束点之间平滑移动
                float t = elapsedTime / moveDuration;
                part.transform.localPosition = Vector3.Lerp(partInfo.GetPosFrom(), partInfo.GetPosTo(), t);
                part.transform.localEulerAngles = Vector3.Lerp(partInfo.GetRotFrom(), partInfo.GetRotTo(), t);
            }
        }

        if(isBack && !isPause)
        {
            elapsedTime += Time.deltaTime;
            if (elapsedTime >= moveDuration)
            {
                isBack = false;
                // 移动完成
                Debug.Log("back move complete");
            }
            else
            {
                // 在起始点和结束点之间平滑移动
                float t = elapsedTime / moveDuration;
                part.transform.localPosition = Vector3.Lerp(partInfo.GetPosTo(), partInfo.GetPosFrom(), t);
                part.transform.localEulerAngles = Vector3.Lerp(partInfo.GetRotTo(), partInfo.GetRotFrom(), t);
            }
        }
    }

    void OnValueChanged(float value)
    {
        part.transform.position = Vector3.Lerp(partInfo.GetPosFrom(), partInfo.GetPosTo(), value);
        part.transform.localEulerAngles = Vector3.Lerp(partInfo.GetRotFrom(), partInfo.GetRotTo(), value);
    }
}